using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponEffectWatcher : MonoBehaviour
{
	public GameObject fireEffectPrefab;

	public GameObject electroEffectPrefab;

	public GameObject freezeEffectPrefab;

	public GameObject stunEffectPrefab;

	public GameObject toxicEffectPrefab;

	public Humanoid humanoid;

	public PlayerBehavior playerBehavior;

	public List<WeaponEffect> currentEffects = new List<WeaponEffect>();

	public bool fireImmun;

	public bool electroImmun;

	public bool freezeImmun;

	public bool stunImmun;

	public bool toxicImmun;

	public void unsetFreezeImmunDelayd()
	{
		StartCoroutine(unsetFreezeImmun());
	}

	public void unsetStunImmunDelayd()
	{
		StartCoroutine(unsetStunImmun());
	}

	public IEnumerator unsetFreezeImmun()
	{
		yield return new WaitForSeconds(3f);
		freezeImmun = false;
	}

	public IEnumerator unsetStunImmun()
	{
		yield return new WaitForSeconds(3f);
		stunImmun = false;
	}

	private void Awake()
	{
		if ((bool)GetComponent<Humanoid>())
		{
			humanoid = GetComponent<Humanoid>();
			if ((bool)GetComponent<PlayerBehavior>())
			{
				playerBehavior = GetComponent<PlayerBehavior>();
			}
		}
		else
		{
			BCWDebug.LogWarning("Effect target must be humanoid!");
		}
	}

	public void SetImmun(EffectType type)
	{
		switch (type)
		{
		case EffectType.Fire:
			fireImmun = true;
			break;
		case EffectType.Electro:
			electroImmun = true;
			break;
		case EffectType.Freeze:
			StopCoroutine(unsetFreezeImmun());
			freezeImmun = true;
			break;
		case EffectType.Stun:
			StopCoroutine(unsetStunImmun());
			stunImmun = true;
			break;
		case EffectType.Toxic:
			toxicImmun = true;
			break;
		case EffectType.Non:
			fireImmun = false;
			electroImmun = false;
			freezeImmun = false;
			stunImmun = false;
			toxicImmun = false;
			break;
		}
		StopEffect(type);
	}

	public bool AddNewEffect(WeaponEffectSettings effectSettings)
	{
		if (!EffectExist(effectSettings))
		{
			switch (effectSettings.effectType)
			{
			case EffectType.Fire:
				if (fireImmun)
				{
					break;
				}
				SetupWeaponEffect(effectSettings, fireEffectPrefab);
				if ((bool)playerBehavior)
				{
					if (!playerBehavior.isMine && effectSettings.killerID == GameController.instance.playerScript.viewId)
					{
						AllUnitaryAchievements.instance.setWeponeffect_200_Fire.Increment();
					}
				}
				else if (effectSettings.killerID == GameController.instance.playerScript.viewId)
				{
					AllUnitaryAchievements.instance.setWeponeffect_200_Fire.Increment();
				}
				return true;
			case EffectType.Toxic:
				if (!toxicImmun)
				{
					SetupWeaponEffect(effectSettings, toxicEffectPrefab);
					return true;
				}
				break;
			case EffectType.Electro:
				if (!electroImmun)
				{
					SetupWeaponEffect(effectSettings, electroEffectPrefab);
					return true;
				}
				break;
			case EffectType.Freeze:
				if (freezeImmun)
				{
					break;
				}
				SetupWeaponEffect(effectSettings, freezeEffectPrefab);
				if ((bool)playerBehavior)
				{
					if (!playerBehavior.isMine && effectSettings.killerID == GameController.instance.playerScript.viewId)
					{
						AllUnitaryAchievements.instance.setWeponeffect_200_Freeze.Increment();
					}
				}
				else if (effectSettings.killerID == GameController.instance.playerScript.viewId)
				{
					AllUnitaryAchievements.instance.setWeponeffect_200_Freeze.Increment();
				}
				return true;
			case EffectType.Stun:
				if (!stunImmun)
				{
					SetupWeaponEffect(effectSettings, stunEffectPrefab);
					return true;
				}
				break;
			}
		}
		else
		{
			BCWDebug.Log(string.Concat("Effect of type ", effectSettings.effectType, " already applied to this player!"));
		}
		return false;
	}

	private WeaponEffect SetupWeaponEffect(WeaponEffectSettings effectSettings, GameObject effectPrefab)
	{
		GameObject gameObject = Object.Instantiate(effectPrefab);
		WeaponEffect component = gameObject.GetComponent<WeaponEffect>();
		gameObject.transform.SetParent(base.transform);
		gameObject.transform.localPosition = Vector3.zero;
		component.settings = effectSettings;
		component.effectWatcher = this;
		if ((bool)humanoid)
		{
			component.humanoid = humanoid;
		}
		currentEffects.Add(component);
		return component;
	}

	private bool EffectExist(WeaponEffectSettings effect)
	{
		foreach (WeaponEffect currentEffect in currentEffects)
		{
			if (currentEffect.settings.effectType == effect.effectType)
			{
				return true;
			}
		}
		return false;
	}

	public bool CanActivateControls()
	{
		foreach (WeaponEffect currentEffect in currentEffects)
		{
			if (currentEffect.disableControls)
			{
				return false;
			}
		}
		return true;
	}

	public void StopEffect(EffectType type)
	{
		foreach (WeaponEffect currentEffect in currentEffects)
		{
			if (type == currentEffect.settings.effectType)
			{
				currentEffect.End();
			}
		}
	}

	public void StopAllEffects()
	{
		foreach (WeaponEffect currentEffect in currentEffects)
		{
			currentEffect.End();
		}
	}

	public void StopAllEffectsImmediate()
	{
		foreach (WeaponEffect currentEffect in currentEffects)
		{
			currentEffect.DestroySelf();
		}
	}
}
